<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="$refs['add-role'].isShow = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border="">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="openDialog(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="$refs['add-role'].getInfoData(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page.sync="query.page"
                :page-sizes="[2,3,4,5]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                background
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <AddRole ref="add-role" @update="initData" />
    <AuthSet ref="auth-set" />
  </div>
</template>

<script>
import AddRole from './components/add-role'
import AuthSet from './components/auth-set.vue'
import { getRolesList, getCompanyInfo, delRoles } from '@/api/settings'
export default {
  name: 'Setting',
  components: {
    AddRole,
    AuthSet
  },
  data() {
    return {
      list: [],
      query: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      formData: {}
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取角色列表和公司信息
    async initData() {
      const { data } = await getRolesList(this.query)
      const res = await getCompanyInfo(this.$store.getters.companyId)
      this.formData = res.data
      console.log(data)
      this.total = data.total
      this.list = data.rows
    },
    sizeChange(val) {
      this.query.pagesize = val
      this.initData()
    },
    currentChange(val) {
      this.query.page = val
      this.initData()
    },
    // 删除角色
    async del(id) {
      await this.$confirm('确认删除吗？')
      await delRoles(id)
      this.$message.success('删除成功')
      if (this.query.page !== 1 && this.list.length === 1) {
        this.query.page--
      }
      this.initData()
    },
    // 打开分配权限
    async openDialog(id) {
      await this.$refs['auth-set'].getRoleAuth(id)
      await this.$refs['auth-set'].getPermissionList()
      this.$refs['auth-set'].showPermDialog = true
    }
  }
}
</script>

